package com.java.controller.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.java.mapper.TMapper;
import com.java.pojo.User;
import com.java.service.UserService;
import com.java.util.PageUtil;

@Controller
@RequestMapping("/user")
@Scope("prototype")
public class AdminController {
	@Autowired
	UserService userService;

	@Autowired
	private TMapper tMapper;

	/**
	 * 分页查询数据
	 * 
	 * @param page
	 * @param limit
	 * @param account
	 * @return
	 */
	@RequestMapping(value = "/all", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
	@ResponseBody
	private Map<String, Object> listAll(@RequestParam("page") String page, @RequestParam("limit") String limit,
			@RequestParam(name = "account", defaultValue = "") String account) {
		Map<String, Object> map = new HashMap<String, Object>();// 查询map
//		int page1 = Integer.parseInt(page);
//		int limit1 = Integer.parseInt(limit);
//		int count = tMapper.record();
//		int page_temp = page1;
//		int limit_temp = limit1;
//		if (count < page1 * limit1) {//判断显示数量是否大于总数目
//			limit1 = count - (page1 - 1) * limit1;
//		}
//		page1 = (page_temp - 1) * limit_temp;
//      System.out.println(page1+"--"+limit1);
		PageUtil<User> pageUtil = new PageUtil<User>(Integer.parseInt(page), Integer.parseInt(limit), tMapper.record());
		map.put("account", account.trim());// 去除空格传值
		map.put("page", null);// 当前页数起始值
		map.put("limit", null);// 显示数量
		List<User> num = tMapper.listUser(map);// 不分页查询结果
		map.put("page", pageUtil.getStartIndex());// 当前页数起始值
		map.put("limit", pageUtil.getPageSize());// 显示数量
		List<User> data = tMapper.listUser(map);// 分页查询结果
		Map<String, Object> reqMap = new HashMap<String, Object>();// 返回map
		reqMap.put("code", 0);
		reqMap.put("msg", "");
		reqMap.put("count", num.size());// 显示总数目
		reqMap.put("data", data);// 显示结果
		return reqMap;
	}

	/**
	 * 添加数据
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.POST)
	@ResponseBody
	private Map<String, Object> add(@RequestBody User user) {
		System.out.println(user);
		int result = userService.insert(user);
		Map<String, Object> map = new HashMap<String, Object>();
		if (result > 0) {
			map.put("status", 1);
		} else {
			map.put("status", 0);
		}
		return map;
	}

	/**
	 * 删除一条数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseBody
	private Map<String, Object> deleteById(@PathVariable("id") int id) {
		int result = userService.deleteById(id);
		Map<String, Object> map = new HashMap<String, Object>();
		if (result > 0) {
			map.put("status", 1);
		} else {
			map.put("status", 0);
		}
		return map;
	}

	/**
	 * 批量删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/ids", method = RequestMethod.DELETE)
	@ResponseBody
	private Map<String, Object> deleteByIds(@RequestParam("ids") String ids) {
		Map<String, Object> map = new HashMap<String, Object>();
		String a[] = ids.split(",");
		try {
			for (int i = 0; i < a.length; i++) {
				String id = a[i];
				tMapper.deleteByPrimaryKey(Integer.parseInt(id));
			}
			map.put("status", 1);
		} catch (Exception e) {
			e.printStackTrace();
			map.put("code", -1);
			map.put("status", 0);
		}
		return map;
	}

	/**
	 * 更新数据
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "", method = RequestMethod.PUT)
	@ResponseBody
	private Map<String, Object> update(@RequestBody User user) {
		int result = userService.update(user);
		Map<String, Object> map = new HashMap<String, Object>();
		if (result > 0) {
			map.put("status", 1);
		} else {
			map.put("status", 0);
		}
		return map;
	}
}
